package cn.com.Servlet.UserServlet;
import cn.com.controler.User.UserDAO;
import cn.com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "LoginServlet",value = "/UserLogin")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得当前请求的协议
        String scheme = request.getScheme();
        //获得当前请求的主机名
        String ServerName = request.getServerName();
        //获得端口号
        int ServerPort = request.getServerPort();
        //获得当前页面的名字
        String ContextPath = request.getContextPath();
        //定义路径
        String basePath = scheme + "://"+ServerName+":"+ServerPort + ContextPath + "/";


        UserDAO userDAO = new UserDAO();
        //获取登录类型
        String logintype = request.getParameter("logintype");
        if(logintype.equals("mmdl")){        //用户登录
            //获取session
            HttpSession session = request.getSession();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            //获取用户输入的验证码
            String imgcodee = request.getParameter("imgcode");
            String imgcode =  imgcodee.toUpperCase();
            //获取session中的验证码
            String scodee = (String) session.getAttribute("imgcode");
            String scode = scodee.toUpperCase();
            if(imgcode.equals(scode)){
                List upselect = userDAO.upselect(username, password);
                if (upselect.size() != 0) {
                    User user = (User)upselect.get(0);
                    Integer userRoleType = user.getUserRoleType();
                    session.setAttribute("user",user);
                    session.setAttribute("userRoleType",userRoleType);
                    response.sendRedirect("/WebContent/UserFrame/main.jsp");
                }else{
                    response.sendRedirect("/WebContent/HTML/UserLogin.jsp");
                }
            }else{
                response.sendRedirect("/WebContent/HTML/UserLogin.jsp");
            }

        }else if(logintype.equals("zc")){    //用户注册
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("email");
            String year = request.getParameter("year");
            String month = request.getParameter("month");
            String day = request.getParameter("day");
            String tel = request.getParameter("tel");
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String resitime = format.format(date);  //注册时间

            String birth = year+"-"+month+"-"+day;
            UserDAO insertDAO = new UserDAO();
            User use = new User(null,username,password,birth,tel,email,resitime,0);
            int adduser = insertDAO.adduser(use);
            if(adduser == 1){
                response.sendRedirect("/WebContent/HTML/UserLogin.jsp");
            }else{
                response.sendRedirect("/WebContent/HTML/UserRegister.jsp");
            }
        }else if(logintype.equals("yklogin")){
            User user = new User(1,"游客","123456","2000-11-12","1452367894","145263@qq.com","2020-10-01",0);
            HttpSession session = request.getSession();
            session.setAttribute("user",user);
        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
